package cn.houserent.dao.impl;

import cn.houserent.dao.UserDao;
import cn.houserent.entity.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    public UserDaoImpl(){}
    @Autowired
    public UserDaoImpl(SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }
    @Override
    public void save(User user) {
        this.getHibernateTemplate().save(user);
    }

    @Override
    public List<User> getUserByProperty(String property, Object value) {
        //from User u where u.name=:name
        String hql = "from User u where u." + property + " =:" + property;
        return this.getHibernateTemplate().execute(session ->
                session.createQuery(hql).setParameter(property, value).list()
        );
    }
}
